package org.geekbang.work9;

import java.util.ArrayList;
import java.util.EventObject;
import java.util.List;

/**
 * default {@link Publisher} implement.
 *
 * @author leo
 * @date 2021/8/29
 * @since 1.0.0
 */
public class SimplePublisher implements Publisher<EventObject> {

    private final List<EventObjectListener<EventObject>> listeners;

    public SimplePublisher() {
        this.listeners = new ArrayList<>();
    }


    @Override
    public void publish(EventObject event) {
        this.listeners.forEach(listener -> {
            listener.onEvent(event);
        });
    }

    @Override
    public void addListener(EventObjectListener<EventObject> listener) {
        this.listeners.add(listener);
    }

    @Override
    public List<EventObjectListener<EventObject>> getListeners() {
        return this.listeners;
    }
}
